package com.tsing.service.impl;

import com.tsing.common.result.PageResult;
import com.tsing.mapper.AppUserMapper;
import com.tsing.mapper.ArticleMapper;
import com.tsing.mapper.LoveMapper;
import com.tsing.mapper.impl.AppUserMapperImpl;
import com.tsing.mapper.impl.ArticleMapperImpl;
import com.tsing.mapper.impl.LoveMapperImpl;
import com.tsing.pojo.dto.TlUserDTO;
import com.tsing.pojo.entity.TlCollect;
import com.tsing.pojo.entity.TlLove;
import com.tsing.pojo.entity.TlPost;
import com.tsing.pojo.entity.TlUser;
import com.tsing.pojo.vo.AppUserVO;
import com.tsing.pojo.vo.ArticleDetailsVO;
import com.tsing.service.AppUserService;

import java.util.ArrayList;
import java.util.List;

/**
 * @auth 卢孝宇
 * @date 2023/12/13 10:14
 */
public class AppUserServiceImpl implements AppUserService {
    private final ArticleMapper articleMapper = new ArticleMapperImpl();
    private final AppUserMapper appUserMapper = new AppUserMapperImpl();

    private final LoveMapper loveMapper = new LoveMapperImpl();
    @Override
    public AppUserVO list(Integer userId) {
        //查询用户信息
        TlUser user = articleMapper.getUserById(userId);
        //查询用户关注数
        Integer interestCount = appUserMapper.interestCount(userId);
        //查询用户粉丝数
        Integer fansCount = appUserMapper.fansCount(userId);
        //查询用户受赞和收藏数
        Long loveCount = 0L;//点赞数
        //查询用户发的帖子在把所有帖子的受赞数累加得到总的受赞数
        List<TlPost> post = articleMapper.postList(userId);
        for (TlPost tlPost : post) {
            loveCount+=tlPost.getLikeCount();
        }
        //查询收藏数
        Long collectIdCount = appUserMapper.collectCount(userId);
        //获赞和收藏数
        Long likeCount = loveCount+collectIdCount;
        AppUserVO appUserVO = new AppUserVO();
        appUserVO.setUserName(user.getUsername());
        appUserVO.setUserImage(user.getUserImage());
        appUserVO.setBookiPhone(user.getBookiPhone());
        appUserVO.setSex(user.getSex());
        appUserVO.setSignature(user.getSignature());
        appUserVO.setIntroduction(user.getIntroduction());
        appUserVO.setFollowers(interestCount);
        appUserVO.setFans(fansCount);
        appUserVO.setCollections(likeCount);

        return appUserVO;
    }

    @Override
    public PageResult<ArticleDetailsVO> collectList(Integer userId, Integer page, Integer size) {
        PageResult<ArticleDetailsVO> apArticleVOList = new PageResult<>();
        List<ArticleDetailsVO> list = new ArrayList<>();

        //拿到所有当前用户收藏的帖子的信息
        List<TlCollect> postId = appUserMapper.postId(userId,page,size);

        //查询用户收藏数
        Long l = appUserMapper.collectCount(userId);
        //根据收藏的收藏帖子的信息去查找有哪些帖子
        List<TlPost> postList = new ArrayList<>();

        for (TlCollect tlCollect : postId) {
            TlPost tlPost = new TlPost();
            tlPost = articleMapper.getPostById(Integer.parseInt(tlCollect.getPostId().toString()));
            postList.add(tlPost);
        }


        for (TlPost tlPost : postList) {

            ArticleDetailsVO a = new ArticleDetailsVO();

            a.setPostId(tlPost.getPostId());
            a.setImage(tlPost.getImage());
            a.setTitle(tlPost.getTitle());
            a.setLikeCount(tlPost.getLikeCount());
            //根据用户id查找对应的用户数据
            List<TlUser> tlUsers = articleMapper.userList(tlPost.getUserId());
            //获取点赞数
            List<TlLove> tlLove = loveMapper.loveCount(tlPost.getUserId(),tlPost.getPostId());
            a.setLoveCount((long) tlLove.size());

            for (TlUser tlUser : tlUsers) {
                a.setUserName(tlUser.getUsername());
                a.setUserImage(tlUser.getUserImage());
            }
            list.add(a);
        }
        apArticleVOList.setData(list);
        apArticleVOList.setCode(200);
        apArticleVOList.setSize(size);
        apArticleVOList.setTotal(Integer.parseInt(l.toString()));
        apArticleVOList.setCurrentPage(page);
        return apArticleVOList;
    }

    @Override
    public PageResult<ArticleDetailsVO> noteList(Integer userId, Integer page, Integer size) {
        PageResult<ArticleDetailsVO> apArticleVOList = new PageResult<>();
        List<ArticleDetailsVO> list = new ArrayList<>();

        //分页查询用户的作品（笔记）
        List<TlPost> postList = appUserMapper.noteList(userId,page,size);

        //查询用户笔记的所有记录条数
        Integer count = appUserMapper.countAll(userId);
        for (TlPost tlPost : postList) {

            ArticleDetailsVO a = new ArticleDetailsVO();

            a.setPostId(tlPost.getPostId());
            a.setImage(tlPost.getImage());
            a.setTitle(tlPost.getTitle());
            a.setLikeCount(tlPost.getLikeCount());
            //根据用户id查找对应的用户数据
            List<TlUser> tlUsers = articleMapper.userList(tlPost.getUserId());
            //获取点赞数
            List<TlLove> tlLove = loveMapper.loveCount(tlPost.getUserId(),tlPost.getPostId());
            a.setLoveCount((long) tlLove.size());

            for (TlUser tlUser : tlUsers) {
                a.setUserName(tlUser.getUsername());
                a.setUserImage(tlUser.getUserImage());
            }
            list.add(a);
        }
        apArticleVOList.setData(list);
        apArticleVOList.setCode(200);
        apArticleVOList.setSize(size);
        apArticleVOList.setTotal(count);
        apArticleVOList.setCurrentPage(page);
        return apArticleVOList;
    }

    @Override
    public Integer updata(TlUserDTO tlUserDTO) {
        return appUserMapper.updateUser(tlUserDTO);
    }
}
